﻿<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO" %>
<script RunAt="server">

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        StreamReader docfile = new StreamReader(Server.MapPath("" + DAL.EnvInfo.PathRoot + "Dem.txt"));
        Application["Visited"] = int.Parse(docfile.ReadLine());
        docfile.Close();
    }
    void Application_End(object sender, EventArgs e)
    {
        //  Code that runs on application shutdown

    }
    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs
    }
    void Session_Start(object sender, EventArgs e)
    { 
        //Code that runs when a new session is started 
        Application.Lock();
        try
        {
            if (Application["Visited"] == null)
            {
                Application["Visited"] = 1;
            }
            else
            {
                Application["Visited"] = (int)Application["Visited"] + 1;
            }
            StreamWriter ghifile = new StreamWriter(Server.MapPath("" + DAL.EnvInfo.PathRoot + "Dem.txt"));
            ghifile.WriteLine(Application["Visited"].ToString());
            ghifile.Close();
            if (Application["Online"] == null)
            {
                Application["Online"] = 1;
            }
            else
            {
                Application["Online"] = (int)Application["Online"] + 1;
            }
        }
        catch (Exception ex) { }
        Application.UnLock();
    }

    void Session_End(object sender, EventArgs e)
    {
         //Code that runs when a session ends. 
         //Note: The Session_End event is raised only when the sessionstate mode
         //is set to InProc in the Web.config file. If session mode is set to StateServer 
         //or SQLServer, the event is not raised.
        Application.Lock();
        Application["Online"] = (int)Application["Online"] - 1;
        Application.UnLock();
    }
    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext myContext = HttpContext.Current;
                   
            //see if we need to rewrite the URL
            string pathRequest = myContext.Request.Path;
            if (BLL.CommonBLL.CheckPattern(pathRequest, BLL.CommonBLL.PatternRequiteUrl))
            {
                try
                {
                    //Match match_rewrite = rewrite_regex.Match(pathRequest);
                    if (BLL.CommonBLL.CheckPattern(pathRequest, BLL.CommonBLL.PatternSearch))
                    {
                        string sResultUrl = "~/search.aspx";
                        string[] ListVal = BLL.CommonBLL.GetValueGroupFromStringByPattern(pathRequest, BLL.CommonBLL.PatternSearch);
                        if (ListVal.Length > 0)
                        {
                            sResultUrl += "?key=" + ListVal[2] + "&siteid=" + ListVal[1];
                            myContext.RewritePath(sResultUrl);
                            return;
                        }
                    }
                    if (BLL.CommonBLL.CheckPattern(pathRequest, BLL.CommonBLL.PatternProductByCategory))
                    {
                        string sResultUrl = "~/products.aspx";
                        string[] ListVal = BLL.CommonBLL.GetValueGroupFromStringByPattern(pathRequest, BLL.CommonBLL.PatternProductByCategory);
                        if (ListVal.Length > 0)
                        {
                            sResultUrl += "?cid=" + ListVal[2] + "&siteid=" + ListVal[1];
                            myContext.RewritePath(sResultUrl);
                            return;
                        }
                    }
                    if (BLL.CommonBLL.CheckPattern(pathRequest, BLL.CommonBLL.PatternProductDetail))
                    {
                        string sResultUrl = "~/productdetail.aspx";
                        string[] ListVal = BLL.CommonBLL.GetValueGroupFromStringByPattern(pathRequest, BLL.CommonBLL.PatternProductDetail);
                        if (ListVal.Length > 0)
                        {
                            sResultUrl += "?cid=" + ListVal[2] + "&pid=" + ListVal[3] + "&siteid=" + ListVal[1];
                            myContext.RewritePath(sResultUrl);
                            return;
                        }
                    }
                    if (BLL.CommonBLL.CheckPattern(pathRequest, BLL.CommonBLL.PatternNewsByCategory))
                    {
                        string sResultUrl = "~/news.aspx";
                        string[] ListVal = BLL.CommonBLL.GetValueGroupFromStringByPattern(pathRequest, BLL.CommonBLL.PatternNewsByCategory);
                        if (ListVal.Length > 0)
                        {
                            sResultUrl += "?gid=" + ListVal[2] + "&siteid=" + ListVal[1];
                            myContext.RewritePath(sResultUrl);
                            return;
                        }
                    }
                    if (BLL.CommonBLL.CheckPattern(pathRequest, BLL.CommonBLL.PatternNewsDetail))
                    {
                        string sResultUrl = "~/newsdetail.aspx";
                        string[] ListVal = BLL.CommonBLL.GetValueGroupFromStringByPattern(pathRequest, BLL.CommonBLL.PatternNewsDetail);
                        if (ListVal.Length > 0)
                        {
                            sResultUrl += "?gid=" + ListVal[2] + "&nid=" + ListVal[3] + "&siteid=" + ListVal[1];
                            myContext.RewritePath(sResultUrl);
                            return;
                        }
                    }
                    if (BLL.CommonBLL.CheckPattern(pathRequest, BLL.CommonBLL.PatternStaticNews))
                    {
                        string sResultUrl = "~/staticnews.aspx";
                        string[] ListVal = BLL.CommonBLL.GetValueGroupFromStringByPattern(pathRequest, BLL.CommonBLL.PatternStaticNews);
                        if (ListVal.Length > 0)
                        {
                            sResultUrl += "?sid=" + ListVal[2] + "&siteid=1";
                            myContext.RewritePath(sResultUrl);
                            return;
                        }
                    }
                    if (BLL.CommonBLL.CheckPattern(pathRequest, BLL.CommonBLL.PatternUrlCart))
                    {
                        string sResultUrl = "~/cart.aspx";
                        string[] ListVal = BLL.CommonBLL.GetValueGroupFromStringByPattern(pathRequest, BLL.CommonBLL.PatternUrlCart);
                        if (ListVal.Length > 0)
                        {
                            sResultUrl += "?siteid=1";
                            myContext.RewritePath(sResultUrl);
                            return;
                        }
                    }
                    if (BLL.CommonBLL.CheckPattern(pathRequest, BLL.CommonBLL.PatternUrlContacForm))
                    {
                        string sResultUrl = "~/contact.aspx";
                        string[] ListVal = BLL.CommonBLL.GetValueGroupFromStringByPattern(pathRequest, BLL.CommonBLL.PatternUrlContacForm);
                        if (ListVal.Length > 0)
                        {
                            sResultUrl += "?siteid=1";
                            myContext.RewritePath(sResultUrl);
                            return;
                        }
                    }
                    if (BLL.CommonBLL.CheckPattern(pathRequest, BLL.CommonBLL.PatternRedirectDefaultPage))
                    {
                        string sResultUrl = "~/default.aspx";
                        string[] ListVal = BLL.CommonBLL.GetValueGroupFromStringByPattern(pathRequest, BLL.CommonBLL.PatternRedirectDefaultPage);
                        if (ListVal.Length > 0)
                        {
                            sResultUrl += "?siteid=1";
                            myContext.RewritePath(sResultUrl);
                            return;
                        }
                    }
                }
                catch (Exception ex) { }
            }        
    }    
</script>